package com.juefeng.SMS.utils.sms.impl;

import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dom4j.Document;
import org.dom4j.Element;

import com.juefeng.SMS.pojo.SmsConfig;
import com.juefeng.SMS.pojo.SmsMessage;
import com.juefeng.SMS.utils.sms.SMSGNErrorCode;
import com.juefeng.SMS.utils.sms.SendSMSUtil;
import com.juefeng.SMS.utils.sms.SmsResult;
import com.juefeng.common.connection.HttpClientHandle;
import com.juefeng.common.utils.XmlUtil;

public class SendSMSUtilGN implements SendSMSUtil {
	private static Log log = LogFactory.getLog(SendSMSUtilGN.class);
	@Override
	public SmsResult sendSMS(SmsMessage message, SmsConfig config) {
		SmsResult smsResult = new SmsResult();
		smsResult.setApiType("GN");
		try {
			String uri = config.getApiUrl();
			HttpClientHandle httpClientHandle = HttpClientHandle.getInstance();
			PostMethod postMethod = new com.juefeng.common.connection.UTF8PostMethod(uri);
			String account = config.getApiUsername();
			String password = config.getApiPossword();
			
			//String extno = "431797";
			String zl = config.getSysSign(); 
			//message.setMessageContent(zl+message.getMessageContent());
			postMethod.setParameter("action", "send");
			postMethod.setParameter("account", account);
			postMethod.setParameter("password", password);
			postMethod.setParameter("mobile", message.getPhoneNos());
			postMethod.setParameter("content",zl+message.getMessageContent());
			//postMethod.setParameter("extno", extno);
			String resultStr =  httpClientHandle.doPostURL(postMethod);
			log.info(" GN  短信执行结果 == " + resultStr);
			Document document = XmlUtil.stringToXML(resultStr); 
			Element rootElement = document.getRootElement();
			if(rootElement != null){
				Element returnstatusElement = rootElement.element("returnstatus");
				if(returnstatusElement != null ){
					String returnstatusText = returnstatusElement.getTextTrim();
					if(returnstatusText.equalsIgnoreCase("Success")){
						
						Element taskIDElement = rootElement.element("taskID");
						if(taskIDElement != null){
							String taskIDText = taskIDElement.getTextTrim();
							smsResult.setMsgId(taskIDText);
						}else{
							log.error("发送即时短信失败 taskIDText return null ！返回值【" + resultStr + "】，手机号码【" + message.getPhoneNos() + "】，短信内容【" + message.getMessageContent() + "】。");
						}
						Element messageElement = rootElement.element("message");
						if(messageElement != null ){
							String messageText = messageElement.getTextTrim();
							if(messageText.equalsIgnoreCase("OK")){
								smsResult.setCode("0");
							}else{
								log.error("发送即时短信失败！返回值【" + resultStr + "】，手机号码【" + message.getPhoneNos() + "】，短信内容【" + message.getMessageContent() + "】。");
								smsResult.setCode(messageText);
								smsResult.setInfo(SMSGNErrorCode.getErrorCodeDesc(messageText));
							}
						}
					}else if(returnstatusText.equalsIgnoreCase("Failed")){
						Element taskIDElement = rootElement.element("taskID");
						if(taskIDElement != null){
							String taskIDText = taskIDElement.getTextTrim();
							smsResult.setMsgId(taskIDText);
						}else{
							log.error("发送即时短信失败 taskIDText return null ！返回值【" + resultStr + "】，手机号码【" + message.getPhoneNos() + "】，短信内容【" + message.getMessageContent() + "】。");
						}
						Element messageElement = rootElement.element("message");
						if(messageElement != null ){
							String messageText = messageElement.getTextTrim();
							if(messageText.equalsIgnoreCase("OK")){
								smsResult.setCode("0");
							}else{
								log.error("发送即时短信失败！返回值【" + resultStr + "】，手机号码【" + message.getPhoneNos() + "】，短信内容【" + message.getMessageContent() + "】。");
								smsResult.setCode(messageText);
								smsResult.setInfo(SMSGNErrorCode.getErrorCodeDesc(messageText));
							}
						}
					}else{
						smsResult.setCode("-1");
					}
				}else{
					
				}
			}else{
				smsResult.setCode("-1");
			}
			
		} catch (Exception e) {
			log.error("发送即时短信失败！手机号码【" + message.getPhoneNos() + "】，短信内容【" + message.getMessageContent() + "】。", e);
			smsResult.setCode("-1");
			smsResult.setInfo("发送即时短信失败"+e.getMessage());
		}
		return smsResult;
	}
	
	public void teste(){
		SmsMessage smsMessage = new SmsMessage();
		smsMessage.setPhoneNos("18518634679");
		smsMessage.setMessageContent("内容");
		SmsConfig smsConfig = new SmsConfig();
		smsConfig.setApiUsername("600015");
		smsConfig.setApiPossword("QcRhzn");
		smsConfig.setApiUrl("http://smgw.guangtongcloud.com:7862/sms");
		
		
		SmsResult sendSMS = sendSMS(smsMessage,smsConfig);
		System.out.println(sendSMS);
	}
}
